<?php

/*
 * file : AdminController
 * author: shuang
 * email : shuangbrother@126.com
 * created_at : 2015-12-8 -- 11:14:02
 */

namespace backend\controllers;

use common\components\BController;
use backend\models\MY_Admin;
use backend\models\MY_Permission;
use yii\helpers\Json;
use common\components\MTools;
use yii\helpers\Url;
use Yii;

class AdminController extends BController {

    public function actions() {
        return [
            "list" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_Admin(),
                "searchParams" => array("id")
            ],
            "delete" => [
                "class" => "\common\actions\DeleteAction",
                "modelClass" => new MY_Admin(),
            ],
            "create" => [
                "class" => "\common\actions\CreateAction",
                "modelClass" => new MY_Admin(),
                "renderParams" => ["action" => "create"],
            ],
            "update" => [
                "class" => "\common\actions\UpdateAction",
                "modelClass" => new MY_Admin(),
                "renderParams" => ["action" => "update"],
                "renderTo" => "create",
                "redirectTo" => "list",
                "redirectParams" => ["id" => ""]
            ],
            "perlist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_Permission(),
                "redirectTo" => "perlist",
                "renderTo" => "perlist"
            ],
            "createper" => [
                "class" => "\common\actions\CreateAction",
                "modelClass" => new MY_Permission(),
                "renderParams" => ["action" => "createper"],
                "renderTo" => "createper",
                "redirectTo" => "perlist",
            ],
            "updateper" => [
                "class" => "\common\actions\UpdateAction",
                "modelClass" => new MY_Permission(),
                "renderParams" => ["action" => "updateper"],
                "renderTo" => "createper",
                "redirectTo" => "perlist",
                "redirectParams" => ["id" => ""]
            ],
            "deleteper" => [
                "class" => "\common\actions\DeleteAction",
                "modelClass" => new MY_Permission(),
                "redirectTo" => "perlist"
            ],
        ];
    }

    public function actionAjaxlist() {
        $res = MY_Admin::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "username" => $item["username"],
                    "email" => $item["email"],
                    "phone" => $item["phone"],
                    "updated_at" => date("Y-m-d", $item["updated_at"]),
                    "action" => MTools::getStringActions([
                        "update" => [
                            "params" => [ "id" => $item["id"]],
                            "title" => "编辑"
                        ],
                        "delete" => [
                            "params" => [ "id" => $item["id"]],
                            "title" => "删除"
                        ]
                    ])
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionAjaxperlist() {
        $res = MY_Permission::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "name" => $item["name"],
                    "updated_at" => date("Y-m-d", $item["updated_at"]),
                    "action" => MTools::getStringActions([
                        "admin/updateper" => [
                            "params" => [ "id" => $item["id"]],
                            "title" => "编辑"
                        ],
                        "admin/deleteper" => [
                            "params" => [ "id" => $item["id"]],
                            "title" => "删除"
                        ]
                    ])
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionChangepass() {
        $model = new \backend\models\ResetPasswordForm();
        if (Yii::$app->request->isPost) {
            if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
                Yii::$app->getSession()->setFlash('success', '修改密码成功');
                return $this->goHome();
            } else {
                Yii::$app->getSession()->setFlash('success', '修改密码失败');
            }
        }
        return $this->render('changepass', ["action" => "changepass", "model" => $model, "labels" => $model->attributeLabels()]);
    }

}
